package com.fsc.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fsc._enum.ReturnEnum;
import com.fsc.entity.ResultWrapper;
import com.fsc.entity.domain.AnnRepoNews;
import com.fsc.entity.domain.LoginInfo;
import com.fsc.entity.domain.StudentActivity;
import com.fsc.entity.dto.AnnRepoNewsDTO;
import com.fsc.entity.dto.NewsDTO;
import com.fsc.entity.dto.NewsEditDTO;
import com.fsc.entity.vo.AnnRepoNewsVO;
import com.fsc.entity.vo.StudentActivityVO;
import com.fsc.handler.BusinessException;
import com.fsc.handler.GlobalExceptionHandler;
import com.fsc.mapper.LoginInfoMapper;
import com.fsc.mapper.StudentActivityMapper;
import com.fsc.service.AnnRepoNewsService;
import com.fsc.mapper.AnnRepoNewsMapper;
import com.fsc.service.StudentActivityService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.util.ArrayList;
import java.util.List;

import static com.fsc._enum.ReturnEnum.ANN_REPO_NEWS_NOT_EXIST;
import static com.fsc._enum.ReturnEnum.SUCCESS;

/**
 * @author 86183
 * @description 针对表【ann_repo_news(通知、公告、新闻表)】的数据库操作Service实现
 * @createDate 2024-06-02 13:15:21
 */
@Service
public class StudentActivityServiceImpl extends ServiceImpl<StudentActivityMapper, StudentActivity>
        implements StudentActivityService {
    @Autowired
    private StudentActivityMapper studentActivityMapper;

    @Autowired
    private LoginInfoMapper loginInfoMapper;

    public ResultWrapper<List<StudentActivityVO>> getNews(int schoolId){
        List<StudentActivity> list = studentActivityMapper.getAll(schoolId);
        List<StudentActivityVO> studentActivityVOList = new ArrayList<>();
        for (StudentActivity studentActivity : list) {
            // 获取authorId
            int authorId = studentActivity.getAuthorId();
            // 查询LoginInfo
            LoginInfo loginInfo = loginInfoMapper.selectLoginInfo(authorId);
            // 创建AnnRepoNewsVO对象
            StudentActivityVO studentActivityVO = new StudentActivityVO();
            // 复制AnnRepoNews的字段到AnnRepoNewsVO
            BeanUtils.copyProperties(studentActivity, studentActivityVO);
            // 设置LoginInfo的字段到AnnRepoNewsVO
            if (loginInfo != null) {
                studentActivityVO.setUsername(loginInfo.getUsername());
                studentActivityVO.setIconUrl(loginInfo.getIconUrl());
            }
            // 添加到结果列表
            studentActivityVOList.add(studentActivityVO);
        }
//        System.out.println(annRepoNewsVOList);
        return ResultWrapper.success(SUCCESS,studentActivityVOList);
    }

    public ResultWrapper<List<StudentActivity>> getActivity(int schoolId,int authorId){
        List<StudentActivity> list = studentActivityMapper.getActivity(schoolId,authorId);
        return ResultWrapper.success(SUCCESS,list);
    }
}
